package cn.qn.service.impl.sys;

import cn.qn.base.PageVO;
import cn.qn.dto.sys.SysMessageDTO;
import cn.qn.dto.sys.SysMessageRequestDTO;
import cn.qn.enums.BooleanEnum;
import cn.qn.mapper.sys.SysMessageMapper;
import cn.qn.moudle.sys.SysMessage;
import cn.qn.repository.sys.SysMessageRepository;
import cn.qn.service.sys.SysMessageService;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Jeet
 * @description
 * @date 2019/7/26 17:13
 */
@Service
@com.alibaba.dubbo.config.annotation.Service(interfaceClass = SysMessageService.class)
public class SysMessageImpl implements SysMessageService {

    @Autowired
    private SysMessageRepository sysMessageRepository;
    @Resource
    private SysMessageMapper messageMapper;

    @Override
    public SysMessage insert(SysMessage message) {
        return sysMessageRepository.save(message);
    }

    @Override
    public PageVO<SysMessageDTO> page(SysMessageRequestDTO request) {
        PageBounds pb = request.getPageBounds();
        List<SysMessageDTO> list = messageMapper.page(pb,request);
        Integer pageCount = messageMapper.pageCount(request);
        return PageVO.convert(pb.getPage(),pb.getLimit(),list,pageCount);
    }

    @Override
    public SysMessageDTO findNewMessage(String userId) {
        return messageMapper.findNewMessage(userId);
    }

    @Override
    public SysMessage findByTitle(String title) {
        return messageMapper.findByTitle(title);
    }

    @Override
    public Integer countByUserIdUnReadMsg(String userId) {
        return sysMessageRepository.countByUserIdAndHasRead(userId,BooleanEnum.FALSE.getStringCode());
    }

}
